<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="98px"
    >
      <el-form-item label="用户手机号" prop="ipaddr">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入用户手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户等级" prop="userName">
        <el-select
          v-model="queryParams.grade"
          clearable
          placeholder="请选择用户等级"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="list" style="width: 100%">
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="用户ID"
        align="center"
        prop="id"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="用户昵称" align="center" prop="name" />
      <el-table-column
        label="用户手机号"
        align="center"
        prop="phone"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="用户等级" align="center">
        <template slot-scope="scope">
          <div v-html="`${scope.row.grade}级`"></div>
        </template>
      </el-table-column>
      <el-table-column
        label="注册时间"
        align="center"
        prop="registration_time"
      />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <!-- "state": "" //状态（0=正常，1=锁定） -->
          <div v-html="`${scope.row.state == 0 ? '正常' : '锁定'}`"></div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleCheck(scope.row)"
            v-hasPermi="['business:miniuser:list']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleGrade(scope.row)"
            v-hasPermi="['business:miniuser:edit']"
            >修改等级</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pageNum"
      :limit.sync="pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="修改用户等级"
      :visible.sync="dialogFormVisible"
      width="400px"
    >
      <el-form :model="form">
        <el-form-item label="用户等级" prop="grade" label-width="100px">
          <el-select v-model="form.grade" placeholder="请选择用户等级">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveGrade">保 存</el-button>
      </div>
    </el-dialog>

    <el-dialog title="查看" :visible.sync="detailVisible" width="400px">
      <el-form :model="form">
        <el-form-item label="用户ID：" prop="id" label-width="100px">
          <div v-html="form.id"></div>
        </el-form-item>
        <el-form-item label="用户昵称：" prop="name" label-width="100px">
          <div v-html="form.name"></div>
        </el-form-item>
        <el-form-item label="用户手机号：" label-width="100px">
          <div v-html="form.phone"></div>
        </el-form-item>
        <el-form-item label="注册时间：" label-width="100px">
          <div v-html="form.registration_time"></div>
        </el-form-item>
        <el-form-item label="状态：" label-width="100px">
          <div v-if="form.state == 0">正常</div>
          <div v-else>锁定</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="detailVisible = false"
          >确 认</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, editState, editGrade } from "@/api/users/list";

export default {
  name: "Users",
  data() {
    return {
      dialogFormVisible: false,
      detailVisible: false,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      pageNum: 1,
      pageSize: 10,
      // 查询参数
      queryParams: {
        phone: undefined,
        grade: undefined,
      },
      options: [
        { value: 1, label: "1 级" },
        { value: 2, label: "2 级" },
        { value: 3, label: "3 级" },
        { value: 4, label: "4 级" },
        { value: 5, label: "5 级" },
      ],
      form: {
        id: "",
        grade: 1,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 查看
    handleCheck(row) {
      this.detailVisible = true;
      this.form = row;
    },
    // 修改等级
    handleGrade(row) {
      this.form.id = row.id;
      this.form.grade = row.grade;
      this.dialogFormVisible = true;
    },
    saveGrade() {
      editGrade(this.form).then((response) => {
        this.dialogFormVisible = false;
        this.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      list({
        ...this.queryParams,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      }).then((response) => {
        console.log("response", response);
        this.list = response?.data?.records;
        this.total = response?.data?.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 强退按钮操作 */
    handleForceLogout(row) {
      this.$confirm(
        '是否确认强退名称为"' + row.userName + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return forceLogout(row.tokenId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("强退成功");
        });
    },
  },
};
</script>
